﻿using ServerBase.Client;
using ServerBase.Config;
using ServerBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UtilPublic.ApiDispatcher;

namespace ToolGame
{
    /// <summary>
    /// PageLogin.xaml 的交互逻辑
    /// </summary>
    public partial class PageLogin : Page
    {
        public PageLogin()
        {
            InitializeComponent();

            ComboBoxServer.ComboBox.ItemsSource = (from kvp in Conf.ConfToolServerGameExt
                                                   where ((EServerType)kvp.Value.ServerType == EServerType.游戏 && kvp.Value.ServerOpen)
                                                   select (kvp.Key));
            ComboBoxServer.ComboBox.SelectedIndex = 1;

            MainWindow.绑定控制器(this);
        }

        private void Button_Click_ServerConn(object sender, RoutedEventArgs e)
        {
            App.GmAccount = TextBoxAccount.TextboxText;
            if (!Conf.ConfToolServerGameExt.TryGetValue(ComboBoxServer.ComboBoxText, out var conf))
            {
                return;
            }
            App.ClientNetSingle.Setup(conf.ServerIp, conf.ServerPort);
            if (App.ClientNetSingle.Start())
            {
                App.DebugDebug("连接 成功");
            }
            else
            {
                return;
            }
        }

        [ApiDpPro((short)EProtocolId.G2C_LOGIN_CONNECT)]
        public short OnLoginConnect()
        {
            var Req = App.GetReq(EGtAction.登录);
            App.ClientNetSingle.Send(Req);
            return 0;
        }

        [ApiDpPubIA1((int)EGtAction.登录)]
        public void 登录(G2B_Gt_Action Rsp)
        {
            App.WdMain.ColumnGridNav.Width = new GridLength(150);
            App.WdMain.GridNav.Visibility = Visibility.Visible;
            App.WdMain.PageOn("PagePlayer");
        }


    }
}
